﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;
using YRevitJig;

namespace YArchitech.Plumbing
{
	internal class SprinklerRhombicJig : XUiJigBase
	{
		public SprinklerRhombicJig(UIDocument uiDoc) : base(uiDoc)
		{
		}

		public XYZ startPoint(Selection sel, LayAreaParameter rect, SprinklersParam para)
		{
			XYZ result;
			try
			{
				this.rectParas = rect;
				this.sprinklersPara = para;
				this.startMin = sel.PickPoint("选择起始点坐标，此点为矩形的基点");
				result = this.startMin;
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public XYZ endPoint(Selection sel)
		{
			XYZ result;
			try
			{
				this.bMove = true;
				this.startMax = sel.PickPoint("选择终止点坐标，此点为矩形的对角点");
				while (this.startMin.IsAlmostEqualTo(this.startMax))
				{
					this.startMax = sel.PickPoint("选择终止点坐标，此点为矩形的对角点");
				}
				if (this.GetPoints())
				{
					result = this.startMax;
				}
				else
				{
					result = null;
				}
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			System.Drawing.Point ptCur = this.m_ptCur;
			if (this.m_ptCur.IsEmpty || paintEventArgs == null)
			{
				return;
			}
			if (this.rectParas == null || this.sprinklersPara == null || !this.bMove)
			{
				return;
			}
			try
			{
				this.startMax = base.Client2Revit(this.m_ptCur);
				if (this.startMax != null)
				{
					if (this.GetPoints())
					{
						XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						foreach (XYZ point in this.xyzList)
						{
							new XCircle2D(base.Revit2Client(point), this.nRadius).Draw(g);
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		private bool GetPoints()
		{
			if (this.rectParas.LimitFlag == 1)
			{
                this.xyzList = this.cm.CalculateLayFamilyInstanceForLingXingXYZ(this.startMin, this.startMax, this.rectParas.RowCount, this.rectParas.ColCount, this.rectParas.Ratio, UnitConvert.CovertToAPI(this.sprinklersPara.OffSet * 1000.0, (DisplayUnitType)2), this.sprinklersPara.BeiShu);
			}
			else
			{
                this.xyzList = this.cm.CalculateLaySprinklersForLingXingXYZ(this.startMin, this.startMax, this.rectParas.RowDistance, this.rectParas.ColDistance, this.rectParas.RowDistanceSide, this.rectParas.ColDistanceSide, this.rectParas.LessDistance, UnitConvert.CovertToAPI(this.sprinklersPara.OffSet * 1000.0, (DisplayUnitType)2), this.sprinklersPara.BeiShu);
			}
			return this.xyzList != null && this.xyzList.Count >= 1;
		}

		private ConnectHelper cm = new ConnectHelper();

		private bool bMove;

		private int nRadius = 3;

		private LayAreaParameter rectParas;

		private SprinklersParam sprinklersPara;

		private XYZ startMin;

		private XYZ startMax;

		public List<XYZ> xyzList = new List<XYZ>();
	}
}
